<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of admin
 *
 * @author Nazmul
 */
class Admin extends CI_Controller {

    var $headerInfo = array();

    public function __construct() {
        parent::__construct();
        $this->headerInfo = $this->pageinfo->getHeaderInfo();
    }

    // this is check login varifiction
    public function _remap($method, $params = array()) {
        if ($this->userauth->isLoggedIn()) { // check login then run below method and code otherwish not
            if (method_exists($this, $method)) { // if exist then ok 
                $this->$method(); // call your needed method
            } else { // if not show page not found error message
                show_404();
            }
        } else { // if user not login then set show this error message
            show_error("403 Forbidden You don't have permission to access / on this server", 403, "403 Forbidden");
        }
    }

    public function index() {
        $data = $this->headerInfo['dashboard'];
        $this->load->view('admin/dashboard', $data);
    }

    public function menu() {
        $data = $this->headerInfo['menu'];
        $this->load->view('admin/addeditmenu', $data);
    }

    public function menulist() {
        $data = $this->headerInfo['menulist'];
        $this->load->view('admin/menulist', $data);
    }

    public function content() {
        $data = $this->headerInfo['content'];
        $this->load->view('admin/addeditcontent', $data);
    }

    public function contentlist() {
        $data = $this->headerInfo['contentlist'];
        $this->load->view('admin/contentlist', $data);
    }

    public function contenttype() {
        $data = $this->headerInfo['content'];
        if ($this->input->post()) {
            $isSuccess = false;
            $result = array();
            $result['message'] = "";
            $result['success'] = false;
            //check validation
            $dod = $this->input->post('dodelete');
            if($dod != 'true'){
                $this->form_validation->set_rules('type', 'Type', 'required|trim');
            }
            //## validation
            if ($this->form_validation->run() === TRUE) { // is form validation ok then 
                $type = $this->input->post('type');
                $ctid = $this->input->post('ctid');
                if ($dod == true) {
                    $isSuccess = $this->contentmodel->updateContentType("", $ctid, true);
                    $message = "Successfully deleted.";
                } else {
                    // save db
                    if (empty($ctid) || $ctid == 0) {
                        $isSuccess = $this->contentmodel->addContentType($type);
                        $message = "Successfully added.";
                    } else {
                        $isSuccess = $this->contentmodel->updateContentType($type, $ctid);
                        $message = "Successfully updated.";
                    }
                }

                if ($isSuccess) {
                    $result['message'] = $message;
                    $result['success'] = true;
                } else {
                    $result['message'] = "We are not able to your action, please try again.";
                }
            } else {
                // set error message from validation
                $result['message'] = validation_errors('<span>', '</span>');
            }
            print json_encode($result);
        } else if ($this->input->get()) {
            $array = array();
            $ctid = $this->input->get('ctid');
            $array = $this->contentmodel->getContentTypeById($ctid);
            $array['ctid'] = $ctid;
            $data = array_merge($data, $array);
            $this->load->view('admin/contenttype', $data);
        } else {
            $this->load->view('admin/contenttype', $data);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */